package ws.jdbc.concurrent.exception;

import ws.jdbc.concurrent.eresultset.EResultSet;

/**
 * This exception is thrown whenever an invariant is broken.
 * 
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class JdbcConcurrentException extends Exception {

  /** The maximum number of possible cursors have been created. The {@link EResultSet} object cannot create more. */
  public static final int ERESULTSET_TOO_MUCH_CURSORS = 0;
  /** Attempt to create a scrollable cursor for a result set that is not scrollable. */
  public static final int IS_NOT_SCROLLABLE = 1;
  /** Attempt to create a forward only cursor a result set that is not forward only. */
  public static final int IS_NOT_FORWARD_ONLY = 2;
  /** Attempt to create a read only cursor for a result set that is not read only. */
  public static final int IS_NOT_READ_ONLY = 3;
  /** Attempt to create an updatable cursor for a result set that is not updatable. */
  public static final int IS_NOT_UPDATABLE = 4;
  /** Identification of the error that caused the exception. */
  private final int error;
  /** Messages for each error code. */
  private static final String[] messages = {
    "Cannot create more cursors. Limit has been reached.",
    "The result set type is neither TYPE_SCROLLABLE_SENSITIVE or TYPE_SCROLLABLE_INSENSITIVE.",
    "The result set type is not TYPE_FORWARD_ONLY.",
    "The result set concurrency is not CONCUR_READ_ONLY.",
    "The result set concurrency is not CONCUR_UPDATABLE."
  };

  /**
   * Create new instance.
   *
   * @param error identification of the error that caused the exception.
   */
  public JdbcConcurrentException(int error) {
    super(messages[error]);
    validate(error);
    this.error = error;
  }

  /**
   * Get error code.
   *
   * @return identification of the error that caused the exception.
   */
  final public int getError() {
    return error;
  }

  /**
   * Check if error code exists.
   *
   * @param error error code exists.
   */
  private void validate(int error) {
    if (error != ERESULTSET_TOO_MUCH_CURSORS) {
      throw new IllegalArgumentException();
    }
  }
}
